<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	// var_dump($_SESSION);
        // var_dump($count );
        //取问题表   
        $sql = 'select t1.*,t2.username,t2.headface,t3.cname from questions t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state <> "置顶" and t1.state <> "精帖,置顶" order by create_time desc limit 0,1';
        $questions = M()->query($sql);
        $this->assign('questions',$questions);
        // var_dump($Page->firstRow,$Page->listRows);

        //取回复量前12个
        $sql = 'select * from user order by answer_num desc limit 12';
        $users = M()->query($sql);
        $this->assign('users',$users);

        //取置顶帖子
        $sql = 'select t1.*,t2.username,t2.headface,t3.cname from questions t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state = "置顶" or t1.state = "精帖,置顶"';
        $states = M()->query($sql);
        $this->assign('states',$states);
        // var_dump($states);
       
        
        $this->assign('title','首页');
        $this->getTopView();
        $this->getTopQuestion();
        $this->display();
    }

    public function add(){
    	
    	//判断用户是否登陆
    	if ($_SESSION["id"]==null) {
    		$url = U('home/login/index');
    		header( "Location: $url" ); 
    		exit();
    	}

    	$cates = M('category')->select();
    	// var_dump($cates);

    	$tempArr = [];

    	foreach ($cates as $key => $row) {
    		if ($row['fid']==0) {
    			$tempArr[]=$row;
    		}
    	}

    	foreach ($tempArr as $key => $father) {
    		foreach ($cates as $v) {
    			if ($v['fid']==$father['id']) {
    				$tempArr[$key]['son'][]=$v;
    			}
    		}
    	}

    	//var_dump($tempArr);
    	$question = D('Vercode')->getOneVerify();
        $this->assign('title','发布');
    	$this->assign('question',$question);
    	$this->assign('tempArr',$tempArr);
    	$this->display();
    }


    public function newadd()
    {

        // var_dump(I('post.'));
    	$vercode = I('post.vercode');

    	if ($vercode != $_SESSION['answer']) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"人类验证失败"
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	// var_dump($_POST);
    	$data = I('post.');
    	$data['uid']=$_SESSION['id'];
    	$data['create_time']=time();
        $data['cid'] = I('post.class');
		$sql = M('questions')->add($data);
		if ($sql) {
			$arr = array(
				'error'=>1,
				'info'=>'发布成功',
				'url'=>U('home/jie/index',array('id'=>$sql))
				);
			echo json_encode($arr);
		}else{
			$arr = array(
				'error'=>0,
				'info'=>'发布失败');
			echo json_encode($arr);

		}
    }

    public function qqreturn() {
        var_dump('hello');
    }

    public function page() {
        // var_dump($_POST);
        $Pagestart = I('post.start');//从哪里开始
        $strip = 5 ;
        //取问题表   
        $sql = 'select t1.*,t2.username,t2.headface,t3.cname from questions t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state <> "置顶" and t1.state <> "精帖,置顶" order by create_time desc limit '.$Pagestart.','.$strip;
        $newq = M()->query($sql);
        // $this->assign('newq',$newq);
        // var_dump($newq);
        if ($newq) {
            $arr = array('info'=>1,'conter'=>$newq);
            echo json_encode($arr);
        }else{
            $arr = array('info'=>0);
            echo json_encode($arr); 
        }
        
    }

    
    
}


